package com.okaixz.client.controller;

import com.okaixz.client.model.domain.AppUpgradeInfo;
import com.okaixz.client.model.domain.User;
import com.okaixz.client.model.dto.GetAccessTokenParams;
import com.okaixz.client.model.dto.UserDto;
import com.okaixz.client.model.vo.CommonInfoVo;
import com.okaixz.client.service.IAppUpgradeInfoService;
import com.okaixz.client.service.ITokenService;
import com.okaixz.client.service.IUserService;
import com.okaixz.common.annotation.Anonymous;
import com.okaixz.common.annotation.OpLog;
import com.okaixz.common.client.Result;
import com.okaixz.common.constant.OpConstants;
import com.okaixz.common.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  二级菜单管理Controller
 *
 * @author ruoyi
 * @date 2024-08-17
 */
@RestController
@RequestMapping("/login")
@Anonymous
public class LoginController extends BaseController
{
    @Autowired
    private IUserService userService;
    @Autowired
    private IAppUpgradeInfoService appUpgradeInfoService;
    @Autowired
    private ITokenService tokenService;

    /**
     * 获取微信扫码登录链接
     */
    @GetMapping("/wx/qrcode")
    public Result<String> wxQrcode()
    {
        String result=userService.wxQrcode();
        return Result.success(result);
    }
    /**
     * 微信第三方应用登录
     */
    @PostMapping("/wx/login")
    @OpLog(opType = "", opDesc = OpConstants.WX_APP_LOGIN,isSaveResponseData =true)
    public Result<UserDto> getAccessToken(@RequestBody GetAccessTokenParams params)
    {
        UserDto user=userService.wxLogin(params);
        return Result.success(user);
    }

    @PostMapping("/commoninfo")
    public Result<CommonInfoVo> getCommonInfo(){
        User user = userService.getTokenUser();
        AppUpgradeInfo appUpgradeInfo = appUpgradeInfoService.getbyClientName(user.getClientName());
        UserDto userDto = tokenService.refreshUserAndToken(user.getUserId());
        CommonInfoVo result = new CommonInfoVo(userDto,appUpgradeInfo);
        return Result.success(result);
    }




}
